package com.block.vest.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.BankCardInfo;
import com.block.db.entity.MemberInfo;
import com.block.db.mapper.BankCardInfoMapper;
import com.block.db.mapper.MemberInfoMapper;
import com.block.vest.model.dto.BankCardInfoDTO;
import com.block.vest.service.IBankCardInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-18
 */
@Service
public class BankCardInfoServiceImpl extends ServiceImpl<BankCardInfoMapper, BankCardInfo> implements IBankCardInfoService {

    @Resource
    MemberInfoMapper memberInfoMapper;

    @Override
    public ResponseVo updateInfo(BankCardInfoDTO cardInfoDTO) {

        MemberInfo memberInfo = memberInfoMapper.selectById(cardInfoDTO.getUid());
        if (ObjectUtil.isEmpty(memberInfo)){
            return new ResponseVo(AppRspCodeConstant.PARAM_ERROR,"用户ID错误","");
        }

        BankCardInfo bankCardInfo = new BankCardInfo();
        bankCardInfo.setId(cardInfoDTO.getCardId());
        bankCardInfo.setUid(cardInfoDTO.getUid());
        bankCardInfo.setCardNo(cardInfoDTO.getCardNo());
        bankCardInfo.setCardGroup(cardInfoDTO.getCardGroup());
        bankCardInfo.setBillDate(cardInfoDTO.getBillDate());
        bankCardInfo.setPayDate(cardInfoDTO.getPayDate());
        bankCardInfo.setValidDate(cardInfoDTO.getValidDate());
        bankCardInfo.setRemark(cardInfoDTO.getRemark());
        bankCardInfo.setImgUrl(cardInfoDTO.getImgUrl());

        boolean saveRes = this.saveOrUpdate(bankCardInfo);
        if (saveRes){
            return new ResponseVo(AppRspCodeConstant.SUCCESS);
        }
        return new ResponseVo(AppRspCodeConstant.ACTION_ERROR);
    }

    @Override
    public ResponseVo dataList(Long uid) {

        BankCardInfo query = new BankCardInfo();
        query.setUid(uid);
        List<BankCardInfo> dataList = this.list(new QueryWrapper<>(query));

        List<BankCardInfoDTO> resultList = new ArrayList<>();
        for (BankCardInfo bankCardInfo: dataList){
            BankCardInfoDTO cardInfoDTO = new BankCardInfoDTO();
            cardInfoDTO.setCardId(bankCardInfo.getId());
            cardInfoDTO.setUid(bankCardInfo.getUid());
            cardInfoDTO.setCardNo(bankCardInfo.getCardNo());
            cardInfoDTO.setCardGroup(bankCardInfo.getCardGroup());
            cardInfoDTO.setBillDate(bankCardInfo.getBillDate());
            cardInfoDTO.setPayDate(bankCardInfo.getPayDate());
            cardInfoDTO.setValidDate(bankCardInfo.getValidDate());
            cardInfoDTO.setRemark(bankCardInfo.getRemark());
            cardInfoDTO.setImgUrl(bankCardInfo.getImgUrl());
            resultList.add(cardInfoDTO);
        }

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", resultList);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnData);
    }
}
